﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class Training_ScoreTypeData
    {
        /// <summary>
        /// Kiểm tra xóa
        /// </summary>
        /// <param name="t1">Table Training_SubjectClass_ScoreType</param>
        /// <param name="scoTypeKey">Loại điểm</param>
        /// <returns></returns>
        public static bool CheckDelete(this Table<Training_SubjectClass_ScoreType> t1, int scoTypeKey)
        {
            var count = from s in t1
                        where s.Training_ScoreTypeKey == scoTypeKey
                        select s;
            if (count.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }           
        }
        /// <summary>
        /// Lấy danh sách loại điểm của một lớp học phần
        /// </summary>
        /// <param name="t1">Table Training_ScoreType</param>
        /// <param name="t2">Table Training_SubjectClass_ScoreType</param>
        /// <param name="subjectClassKey">Mã lớp học phần</param>
        /// <returns></returns>
        public static IQueryable<Training_ScoreType> GetList(this Table<Training_ScoreType> t1, Table<Training_SubjectClass_ScoreType> t2, int subjectClassKey)
        {
            var scoreTypeList = from stl in t2
                                join st in t1 on stl.Training_ScoreTypeKey equals st.Training_ScoreTypeKey
                                where stl.Training_Subject_ClassKey == subjectClassKey
                                select st;
            return scoreTypeList;
        }

        /// <summary>
        /// (Thi tot nghiep) Danh sach loai diem 
        /// </summary>
        /// <param name="tScoreType"></param>
        /// <param name="tSubject"></param>
        /// <param name="iSubjectExamKey"></param>
        /// <returns></returns>
        public static IQueryable GetListScoreTypeBySubjectExam(this Table<Training_ScoreType> tScoreType,
                                                    Table<Training_Subject> tSubject, int iSubjectExamKey)
        {
            var l = from scoreType in tScoreType
                    from subject in tSubject

                    where scoreType.Training_SubjectKey == subject.Training_SubjectKey
                    where scoreType.Graduate_SubjectExamKey == iSubjectExamKey

                    select new
                    {
                        scoreType.Training_ScoreTypeKey,
                        scoreType.Coefficient,
                        scoreType.Name,
                        scoreType.Graduate_SubjectExamKey,
                        SubjectExam = scoreType.SubjectExam == null ? false : scoreType.SubjectExam,
                        SubjectCondition = scoreType.SubjectCondition == null ? false : scoreType.SubjectCondition,
                        scoreType.SubjectNameAlias,
                        scoreType.SubjectThesis,
                        subject.Training_SubjectKey,
                        subjectID = subject.ID,
                        subjectName = subject.Name
                    };
            return l;
        }

        #region
        public static IQueryable GetData(this Table<Training_ScoreType> nTraining_ScoreType)
        {
            var value = from t in nTraining_ScoreType
                        orderby
                            t.Training_ScoreTypeKey
                        select new
                        {
                            t.Training_ScoreTypeKey,
                            t.Name
                        };
            return value.Distinct();
        }
        #endregion
    }
}
